package Servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/ServletCookie03")
public class ServletCookie03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
      //获取cookie
        Cookie[] cookies=request.getCookies();
        if (cookies==null){
            //表明我第一次访问
            Cookie c=new Cookie("lasttime",System.currentTimeMillis()+"");
            c.setMaxAge(10);
            response.addCookie(c);
            response.getWriter().write("第一次查看的时间");
        }else {
            //第二次访问
            //获取以前的cookie
            for (Cookie cookie:cookies) {
                String value = cookie.getValue();
        response.getWriter().write("欢饮再次访问，"+"上次访问的时间是："+value);

            }

        }
    }
}
